package edu.hm.dako.EchoApplication.Gui;

import java.awt.Color;

import BenchmarkingClient.BenchmarkingGuiSimulation;

/**
 *
 * @author Max
 */
public class JWindow extends javax.swing.JFrame {

	int vNoC, vCtt, vNoM, vLoM;
	String msg;
	
    /** Creates new form NewJFrame */
    public JWindow() {
        initComponents();
    }

    
    // Variables declaration
    private javax.swing.JLabel Label_Ctt;
    private javax.swing.JLabel Label_LoM;
    private javax.swing.JLabel Label_Logo;
    private javax.swing.JLabel Label_Msg;
    private javax.swing.JLabel Label_NoC;
    private javax.swing.JLabel Label_NoM;
    private javax.swing.JLabel Label_Server;
    private javax.swing.JLabel Label_header;
    private javax.swing.JComboBox ServerDD;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JScrollPane outputPane;
    private javax.swing.JButton run_btn;
    private static javax.swing.JTextArea outputArea;
    private javax.swing.JTextField NoC_btn, Ctt_btn, NoM_btn, LoM_btn, Msg_btn;
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        Label_header = new javax.swing.JLabel();
        run_btn = new javax.swing.JButton();
        outputPane = new javax.swing.JScrollPane();
        outputArea = new javax.swing.JTextArea();
        NoC_btn = new javax.swing.JTextField();
        Ctt_btn = new javax.swing.JTextField();
        NoM_btn = new javax.swing.JTextField();
        LoM_btn = new javax.swing.JTextField();
        Msg_btn = new javax.swing.JTextField();
        Label_NoC = new javax.swing.JLabel();
        Label_Ctt = new javax.swing.JLabel();
        Label_NoM = new javax.swing.JLabel();
        Label_LoM = new javax.swing.JLabel();
        ServerDD = new javax.swing.JComboBox();
        Label_Server = new javax.swing.JLabel();
        Label_Msg = new javax.swing.JLabel();
        Label_Logo = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Software Engineering - EchoApplication");

        mainPanel.setBackground(new java.awt.Color(255, 255, 255));

        Label_header.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        Label_header.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        Label_header.setText("Benchmarkingtest");
        Label_header.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        run_btn.setText("Run");
        run_btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                run_btnActionPerformed(evt);
            }
        });

        LoM_btn.setEditable(false);
        LoM_btn.setBackground(Color.GRAY);
        
        outputPane.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        outputArea.setBackground(new java.awt.Color(235, 235, 235));
        outputArea.setColumns(20);
        outputArea.setEditable(false);
        outputArea.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        outputArea.setRows(5);
        outputArea.setBorder(null);
        outputArea.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        outputPane.setViewportView(outputArea);


        Label_NoC.setText("Number of Clients:");
        Label_Ctt.setText("Client Thinktime:");
        Label_NoM.setText("Number of Messages:");
        Label_LoM.setText("Length of Messages:");
        Label_Server.setText("Server:");
        Label_Msg.setText("Message:");
        
        ServerDD.setModel(new javax.swing.DefaultComboBoxModel(new String[] 
              { " ", 
        		"TCP/IP single-threaded", 
        		"TCP/IP multi-threaded", 
        		"UDP single-threaded", 
        		"UDP multi-threaded", 
        		"RMI ", 
        		"LWTRT", 
        	  }));
        
        ServerDD.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ServerDDActionPerformed(evt);
            }
        });

        Label_Logo.setBackground(new java.awt.Color(204, 204, 204));
        Label_Logo.setIcon(new javax.swing.ImageIcon("src/main/java/edu/hm/dako/EchoApplication/Gui/PiM.jpg"));

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, mainPanelLayout.createSequentialGroup()
                                .addComponent(Label_header, javax.swing.GroupLayout.DEFAULT_SIZE, 391, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(Label_Logo, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(23, 23, 23))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, mainPanelLayout.createSequentialGroup()
                                .addComponent(Label_Msg)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(Msg_btn, javax.swing.GroupLayout.PREFERRED_SIZE, 327, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(run_btn, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(outputPane, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 516, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap())
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(Label_NoC)
                            .addComponent(Label_Ctt)
                            .addComponent(Label_NoM)
                            .addComponent(Label_LoM))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(LoM_btn)
                            .addComponent(Ctt_btn)
                            .addComponent(NoC_btn, javax.swing.GroupLayout.DEFAULT_SIZE, 45, Short.MAX_VALUE)
                            .addComponent(NoM_btn, javax.swing.GroupLayout.DEFAULT_SIZE, 45, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 91, Short.MAX_VALUE)
                        .addComponent(Label_Server)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ServerDD, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(90, 90, 90))))
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(Label_header)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(Label_NoC)
                            .addComponent(NoC_btn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(Ctt_btn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(Label_Ctt)))
                    .addComponent(Label_Logo, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Label_NoM)
                    .addComponent(NoM_btn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Label_Server)
                    .addComponent(ServerDD, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(LoM_btn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Label_LoM, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Msg_btn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Label_Msg, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(run_btn))
                .addGap(9, 9, 9)
                .addComponent(outputPane, javax.swing.GroupLayout.PREFERRED_SIZE, 274, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jMenu1.setText("File");
        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 507, Short.MAX_VALUE)
        );
        pack();
    }

    private void run_btnActionPerformed(java.awt.event.ActionEvent evt) {
        setvNoC(Integer.parseInt(NoC_btn.getText())); 
        setvCtt(Integer.parseInt(Ctt_btn.getText()));
        setvNoM(Integer.parseInt(NoM_btn.getText()));
        setvLoM((Msg_btn.getText()).length());
        setMsg(Msg_btn.getText());
        new BenchmarkingGuiSimulation().doWork(vNoC, vCtt, vNoM, vLoM, msg); 
    }

    private void ServerDDActionPerformed(java.awt.event.ActionEvent evt) {
    	 new Thread( new Runnable(){
             public void run() {
             	int selectedIndex = ServerDD.getSelectedIndex();
            	if(selectedIndex == 1){
                	new edu.hm.dako.EchoApplication.TCPSingleThreaded.TCPSingleThreadedEchoServer();    		
            	}
             }
           }).start();
    }

	public int getvNoC() {
		return vNoC;
	}
	public void setvNoC(int vNoC) {
		this.vNoC = vNoC;
	}
	public int getvCtt() {
		return vCtt;
	}
	public void setvCtt(int vCtt) {
		this.vCtt = vCtt;
	}
	public int getvNoM() {
		return vNoM;
	}
	public void setvNoM(int vNoM) {
		this.vNoM = vNoM;
	}
	public int getvLoM() {
		return vLoM;
	}
	public void setvLoM(int vLoM) {
		this.vLoM = vLoM;
	}
	public static javax.swing.JTextArea getOutputArea() {
		return outputArea;
	}
	public void setOutputArea(javax.swing.JTextArea outputArea) {
		this.outputArea = outputArea;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}
}